package com.qfhd.soft.cjzcore.User;

import android.text.TextUtils;

import com.qfhd.soft.cjzcore.CJZConstant;
import com.qfhd.soft.cjzcore.Core.CJCrypto;
import com.qfhd.soft.cjzcore.Core.CJHttp;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Core.CJTime;
import com.qfhd.soft.cjzcore.Moudle.CJGGKHelper;
import com.qfhd.soft.cjzcore.Moudle.CJImgTaskHelper;
import com.qfhd.soft.cjzcore.Moudle.CJTryPlaceHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CJZUser {
    private CJZUserInfo userInfo;
    private String token;

    private static CJZUser user;

    public CJZUserInfo getUserInfo() {
        return userInfo;
    }

    public static CJZUser getInstance() {
        if (user == null) {
            synchronized (CJZUser.class) {
                user = new CJZUser();
            }
        }
        return user;
    }

    public void addCoin(int number) {
        getUserInfo().score += number;
        getUserInfo().dailyScore += number;
    }

    public void addCash(float amount) {
        getUserInfo().balance += amount;
    }

    public String getToken(){
        return token;
    }

    private String changeToJson(String text) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("data", CJCrypto.GetEncryptString(text));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject.toString();
    }

    public String DeencryStr(String text) {
        try {
            JSONObject obj = new JSONObject(text);
            if (obj.has("res")) {
                String result = obj.getString("res");
                return CJCrypto.GetDecryptoString(result);
            }
        } catch (JSONException e) {
            CJLogger.v("DeencryStr ：" + e.toString());
        }
        return null;
    }

    public JSONObject responMethod(String response, String methodName) {
        if (TextUtils.isEmpty(response)) return null;
        try {
            JSONObject jsonRespon = new JSONObject(response);
            if (jsonRespon.has("time_stamp")) {
                String time = jsonRespon.getString("time_stamp");
                CJTime.SetServerTimeStamp(Long.parseLong(time));
            }
            if (jsonRespon.has("code")) {
                int code = jsonRespon.getInt("code");
                if (code == 200) {
                    if (jsonRespon.has("data")) {
                        return jsonRespon.getJSONObject("data");
                    }
                }
            }
        } catch (JSONException e) {
            CJLogger.v(methodName + " :" + e.toString());
            return new JSONObject();
        }

        return null;
    }

    public boolean getInfo(JSONArray requests, String paramas) {
        boolean ret = false;
        JSONObject request = new JSONObject();
        try {
            JSONArray jsonRequestArray = new JSONArray();

            request.put("requests", requests);
            request.put("params", paramas);
            CJLogger.v("getInfo request ：" + request.toString());
            String response = CJHttp.getInstance().post(CJZConstant.API.common, changeToJson(request.toString()));
            response = DeencryStr(response);
            CJLogger.v("getInfo resonse :" + response);
            JSONObject dataObj = responMethod(response, "getAdSource");
            if (dataObj != null) {
                ret = true;
                //解析返回格式
                resoluveData(dataObj);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            CJLogger.v("zuanshi_cash error:" + e.toString());
        }
        return ret;
    }

    private void resoluveData(JSONObject data) {

        if (data.has(CJZConstant.API.fuc_deviceLogin)) {//设备登录
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.fuc_deviceLogin);
                token = tmp.getString("token");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_userInfo)){//用户信息
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.fuc_userInfo);
                userInfo = CJZUserInfo.getmodelByJSON(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_imgTaskList)){//注册任务列表
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.fuc_imgTaskList);
                CJZUser.getInstance().getUserInfo().addSignLength();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_ggkList)){//刮刮卡列表
            try {
                JSONArray tmp = data.getJSONArray(CJZConstant.API.fuc_ggkList);
                CJGGKHelper.getInstance().setGGkList(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_ggkDetail)){//刮刮卡列表
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.fuc_ggkDetail);
                CJGGKHelper.getInstance().getCardDetail(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_tryPlace)){//试玩列表
            try {
                JSONArray tmp = data.getJSONArray(CJZConstant.API.fuc_tryPlace);
                CJTryPlaceHelper.getInstance().setTryTaskList(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_tryPlace_getTask)){//试玩列表
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.fuc_tryPlace_getTask);
//                CJTryPlaceHelper.getInstance().setTryTaskList(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_tryPlaceDetail)){//试玩列表
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.fuc_tryPlaceDetail);
                CJTryPlaceHelper.getInstance().setDetailModel(tmp);
//                CJTryPlaceHelper.getInstance().setTryTaskList(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.getFuc_tryPlace_submit)){//试玩列表
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.getFuc_tryPlace_submit);
//                CJTryPlaceHelper.getInstance().setDetailModel(tmp);
//                CJTryPlaceHelper.getInstance().setTryTaskList(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_imgTaskList)){//注册任务列表
            try {
                JSONArray tmp = data.getJSONArray(CJZConstant.API.fuc_imgTaskList);
                CJImgTaskHelper.getInstance().setImgTaskList(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(data.has(CJZConstant.API.fuc_imgTaskDetail)){//注册任务详情
            try {
                JSONObject tmp = data.getJSONObject(CJZConstant.API.fuc_imgTaskDetail);
                CJImgTaskHelper.getInstance().changeToDetailModel(tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
